package com.d3works.apps.base.service.batch;

import com.d3works.apps.base.db.BatchImportHistory;
import com.d3works.apps.base.db.FileSourceConnectorParameters;
import com.d3works.apps.base.db.repo.BatchImportHistoryRepository;
import com.d3works.apps.base.db.repo.BatchRepository;
import com.d3works.apps.base.service.administration.AbstractBatch;
import com.d3works.apps.base.service.filesourceconnector.FileSourceConnectorService;
import com.d3works.apps.base.service.filesourceconnector.models.FileTransfertSession;
import com.d3works.apps.base.translation.ITranslation;
import com.d3works.auth.db.repo.UserRepository;
import com.d3works.exception.D3worksException;
import com.d3works.i18n.I18n;
import com.d3works.meta.db.MetaFile;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
import java.util.List;

public abstract class AbstractImportBatch extends AbstractBatch {

  protected FileSourceConnectorService fileSourceConnectorService;
  protected BatchImportHistoryRepository batchImportHistoryRepository;
  protected UserRepository userRepository;

  @Inject
  protected AbstractImportBatch(
      FileSourceConnectorService fileSourceConnectorService,
      BatchImportHistoryRepository batchImportHistoryRepository,
      UserRepository userRepository) {
    this.fileSourceConnectorService = fileSourceConnectorService;
    this.batchImportHistoryRepository = batchImportHistoryRepository;
    this.userRepository = userRepository;
  }

  protected List<MetaFile> downloadFiles(
      FileSourceConnectorParameters fileSourceConnectorParameters) throws D3worksException {

    FileTransfertSession session =
        fileSourceConnectorService.createSession(
            fileSourceConnectorParameters.getFileSourceConnector());
    return fileSourceConnectorService.download(session, fileSourceConnectorParameters);
  }

  @Override
  protected void stop() {

    StringBuilder comment = new StringBuilder();
    comment.append(
        "\t"
            + String.format(
                I18n.get(ITranslation.BASE_IMPORT_BATCH_FILES_IMPORTED), batch.getDone()));
    comment.append(
        "\t"
            + String.format(
                I18n.get(com.d3works.apps.base.exceptions.IExceptionMessage.ALARM_ENGINE_BATCH_4),
                batch.getAnomaly()));
    addComment(comment.toString());
    super.stop();
  }

  @Transactional
  protected void createBatchHistory(MetaFile dataMetaFile, MetaFile logMetaFile) {
    BatchImportHistory batchImportHistory = new BatchImportHistory();
    batchImportHistory.setBatch(batchRepo.find(getCurrentBatchId()));
    batchImportHistory.setDataMetaFile(dataMetaFile);
    batchImportHistory.setLogMetaFile(logMetaFile);
    if (batch.getCreatedBy() != null) {
      batchImportHistory.setUser(userRepository.find(batch.getCreatedBy().getId()));
    }
    batchImportHistoryRepository.save(batchImportHistory);
  }

  @Override
  protected void setBatchTypeSelect() {
    this.batch.setBatchTypeSelect(BatchRepository.BATCH_TYPE_IMPORT_BATCH);
  }
}
